#include "qdateedithost.h"

#include "qcommonstruct.h"
#include "qabstractproperty.h"
#include "qpropertyfactory.h"

#include <QDateTimeEdit>

using namespace SHAREDLIB;

QDateEditHost::QDateEditHost(QObject *object):
    QAbstractSpinBoxHost(object)
{
    QAbstractProperty *pro;

    pro=QPropertyFactory::createProperty("Date");
    if(pro!=NULL)
    {
        pro->setAttribute("name","maximumDate");
        pro->setAttribute("property_name",tr("MaximumDate"));
        pro->setAttribute("group","Attributes");
        addProperty(pro);
    }

    pro=QPropertyFactory::createProperty("Date");
    if(pro!=NULL)
    {
        pro->setAttribute("name","minimumDate");
        pro->setAttribute("property_name",tr("MinimumDate"));
        pro->setAttribute("group","Attributes");
        addProperty(pro);
    }

    pro=QPropertyFactory::createProperty("Date");
    if(pro!=NULL)
    {
        pro->setAttribute("name","date");
        pro->setAttribute("property_name",tr("Date"));
        pro->setAttribute("group","Attributes");
        addProperty(pro);
    }

    pro=QPropertyFactory::createProperty("Enum");
    if(pro!=NULL)
    {
        pro->setAttribute("name","currentSection");
        pro->setAttribute("property_name",tr("CurrentSection"));
        pro->setAttribute("group","Attributes");
        ComboItems items;
        tagComboItem item;

        item.m_text=tr("YearSection");
        item.m_value=QDateTimeEdit::YearSection;
        items.append(item);

        item.m_text=tr("MonthSection");
        item.m_value=QDateTimeEdit::MonthSection;
        items.append(item);

        item.m_text=tr("SecondSection");
        item.m_value=QDateTimeEdit::DaySection;
        items.append(item);

        QVariant v;
        v.setValue<ComboItems>(items);
        pro->setAttribute("items",v);
        pro->setValue(QDateTimeEdit::YearSection);
        addProperty(pro);
    }


    pro=QPropertyFactory::createProperty("Script");
    if(pro!=NULL)
    {
        pro->setAttribute("name","dateChanged");
        pro->setAttribute("property_name",tr("DateChanged"));
        pro->setAttribute("group","Events");
        m_object->setProperty("dateChanged","");
        addProperty(pro);
    }


    QDateTimeEdit *e=(QDateTimeEdit*)m_object;
    connect(e,SIGNAL(dateChanged(QDate)),this,SLOT(dateChanged(QDate)));
}

void QDateEditHost::setDate(int year, int month, int day)
{
    QDate dt(year,month,day);
    setPropertyValue("date",dt);
}

int QDateEditHost::year()
{
    QDate dt=getPropertyValue("date").toDate();
    return dt.year();
}

int QDateEditHost::month()
{
    QDate dt=getPropertyValue("date").toDate();
    return dt.month();
}

int QDateEditHost::day()
{
    QDate dt=getPropertyValue("date").toDate();
    return dt.day();
}

void QDateEditHost::setCurrentSection(int currentSection)
{
    setPropertyValue("currentSection",currentSection);
}

int QDateEditHost::currentSection()
{
    return getPropertyValue("currentSection").toInt();
}

void QDateEditHost::dateChanged(const QDate &date)
{
    QString code=getPropertyValue("dateChanged").toString();
    if(code!="")
    {
        QMap<QString,QString> param;
        param.insert("year",QString::number(date.year()));
        param.insert("month",QString::number(date.month()));
        param.insert("day",QString::number(date.day()));
        emit_exec(code,param);
    }
}
